<template>
  <div :class="screenClass">
    <table-container
      v-model:columns="columns"
      v-model:is-fullscreen="isFullscreen"
      :loading="isLoading"
      :all-columns="allColumns"
      @refresh="getData"
    >
      <template v-if="props.editFlag" #container-side>
        <a-space :size="8">
          <a-button
            v-permission="['pms:order-detail:saveAll']"
            type="primary"
            @click="add"
            >添加</a-button
          >
          <a-button
            v-permission="['pms:order-detail:updateAll']"
            :disabled="selectedKeys.length === 0"
            @click="batchEdit"
            >批量编辑</a-button
          >
          <batchImport
            v-permission="['pms:order-detail:import']"
            btn-tititle="导入订单明细"
            title="导入订单明细"
            export-type="apiUrl"
            :filter-secret-level="formInfo.secretLevel"
            :fiext-secret-level="formInfo.secretLevel"
            file-code="order_procurementOrderDetailUrl"
            :import-url="`/api/procurement/order/order-detail/${props.formInfo.id}/import`"
            @close-drawer="closeDrawer"
            @finish="finish"
          />
          <visual-angle />
        </a-space>
      </template>
      <editTable
        v-model:selectedKeys="selectedKeys"
        :show-add="false"
        row-key="id"
        :table-data="renderData"
        :row-selection="rowSelection"
        :columns="columns"
        @selection-change="selectionChange"
      >
        <template #operation="{ rowIndex, record }">
          <a-space :size="24">
            <a-button
              v-if="!record.isEdit"
              v-permission="['pms:order-detail:updateAll']"
              type="text"
              style="margin-left: 16px; padding: 0"
              @click="edit(rowIndex, record)"
              >编辑</a-button
            >
            <a-button
              v-if="!record.isEdit"
              v-permission="['pms:order-detail:delete']"
              type="text"
              style="padding: 0"
              @click="del(record)"
              >删除</a-button
            >
          </a-space>
        </template>
      </editTable>
    </table-container>
    <!-- 添加 -->
    <add-detail-dialog
      v-if="addVisible"
      :form-info="formInfo"
      :id-list="idList"
      @close-drawer="closeDrawer"
      @success="success"
    />
    <!-- 批量编辑 -->
    <batch-edit-dialog
      v-if="batchEditVisible"
      :selection="selection"
      :row-data="rowData"
      :edit-type="editType"
      @close-drawer="closeDrawer"
      @success="success"
    />
  </div>
</template>

<script lang="ts" setup>
  import { ref, defineProps, onMounted, defineEmits } from 'vue';
  import tableContainer from '@/common/components/table-container/index.vue';
  import editTable from '@/common/components/edit-table/index.vue';
  import batchImport from '@/common/components/batch-import/index.vue';
  import { info } from '@/common/utils/modal-promise';
  import lodash from 'lodash';
  import useFullScreen from '@/common/hooks/fullScreen';
  // import secretLevelInfo from '@/common/components/secret-level-info/index.vue';
  import {
    getPmsOrderDetailPage,
    delOrderDetail,
  } from '@/api/purchase-order/purchaseOrderPreparation';
  // import inputNum from '@/views/purchase-order/components/input-num.vue';
  import { Message } from '@arco-design/web-vue';
  import { detailTableCols } from './config';
  import visualAngle from './visual-angle.vue';
  import addDetailDialog from './add-detail-dialog.vue';
  import batchEditDialog from './batch-edit-dialog.vue';

  const props = defineProps({
    formInfo: {
      type: Object,
      default: null,
    },
    editFlag: {
      type: Boolean,
      default: true,
    },
  });
  const emit = defineEmits(['refresh']);

  const { isFullscreen, screenClass } = useFullScreen();

  const columns = ref<any[]>([...detailTableCols]);
  if (props.editFlag) {
    columns.value.push({
      title: '操作',
      dataIndex: 'operation',
      width: 150,
      fixed: 'right',
    });
  }
  const rowSelection = {
    type: 'checkbox',
    showCheckedAll: true,
    onlyCurrent: true,
  };
  const selectedKeys = ref([]);
  const selection = ref<any[]>([]);
  const allColumns = lodash.cloneDeep(columns);
  const isLoading = ref(true);
  const batchEditVisible = ref(false);
  const addVisible = ref(false);
  const renderData = ref<any[]>([]);
  const idList = ref<string[]>([]);
  const editType = ref('');
  const rowData = ref();
  const selectionChange = (val: string[]) => {
    selection.value = renderData.value.filter((item) => {
      return val.includes(item.id);
    });
  };
  const getData = async () => {
    selection.value = [];
    selectedKeys.value = [];
    renderData.value = [];
    isLoading.value = true;
    const params = {
      pmsOrderId: props.formInfo.id,
    };
    try {
      const res: any = await getPmsOrderDetailPage(params);
      if (res.code === 200) {
        const arr: string[] = [];
        renderData.value = res.data.list.map((item: any) => {
          arr.push(item.pmsLongtermAgreementLId);
          return item;
        });
        idList.value = [...arr];
      }
    } finally {
      isLoading.value = false;
    }
  };
  // 编辑
  const edit = (index: number, row: any) => {
    editType.value = 'radio';
    rowData.value = { ...row };
    batchEditVisible.value = true;
  };
  // 删除
  const del = async (row: any) => {
    await info({
      title: '您确定要删除当前数据？',
      content: '',
      hideCancel: false,
    });
    const res: any = await delOrderDetail(row.id);
    if (res.code === 200) {
      Message.success('删除成功');
      emit('refresh');
      getData();
    }
  };
  // 添加
  const add = () => {
    addVisible.value = true;
  };
  // 批量编辑
  const batchEdit = () => {
    if (selectedKeys.value.length === 0) {
      Message.warning('请至少选择一条数据编辑！');
      return;
    }
    editType.value = 'batch';
    batchEditVisible.value = true;
  };
  // 关闭弹窗
  const closeDrawer = () => {
    batchEditVisible.value = false;
    addVisible.value = false;
  };
  const finish = () => {
    getData();
  };
  // 关闭弹窗
  const success = () => {
    batchEditVisible.value = false;
    addVisible.value = false;
    getData();
    emit('refresh');
  };
  onMounted(() => {
    getData();
  });
</script>
